package com.sikao.app.ui;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;

import com.sikao.app.R;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.view.photoview.HackyViewPager;
import com.sikao.app.view.photoview.ImageDetailFragment;

/**
 * 图片查看器
 */
public class PictureViewerActivity extends BaseActivity {
  private static final String STATE_POSITION = "STATE_POSITION";
  public static final String EXTRA_IMAGE_INDEX = "image_index";
  public static final String EXTRA_IMAGE_URLS = "image_urls";

  private HackyViewPager mPager;
  private int pagerPosition;
  private TextView indicator;

  private ArrayList<String> urls;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.activity_common_picture_viewer);
    super.onCreate(savedInstanceState);

  }

  @Override
  protected void initData() {
    /*pagerPosition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX, 0);
    urls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);*/
    pagerPosition = 2;
    urls = new ArrayList<>();
    String url = "https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1462094756&di=61b731ee9966aebe5ea5bdcd8c220b2d&src=http://pic16.nipic.com/20110829/2786001_092204054000_2.jpg";
    urls.add(url);
    urls.add(url);
    urls.add(url);
  }

  @Override
  protected void initView() {
    mPager = (HackyViewPager) findViewById(R.id.pager);
    indicator = (TextView) findViewById(R.id.indicator);
  }

  @Override
  protected void initListener() {
    // 更新下标
    mPager.setOnPageChangeListener(new OnPageChangeListener() {

      @Override
      public void onPageScrollStateChanged(int arg0) {
      }

      @Override
      public void onPageScrolled(int arg0, float arg1, int arg2) {
      }

      @Override
      public void onPageSelected(int arg0) {
        CharSequence text = getString(R.string.viewpager_indicator, arg0 + 1, mPager.getAdapter().getCount());
        indicator.setText(text);
      }
    });
  }

  @Override
  protected void init() {
    ImagePagerAdapter mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), urls);
    mPager.setAdapter(mAdapter);

    CharSequence text = getString(R.string.viewpager_indicator, 1, mPager.getAdapter().getCount());
    indicator.setText(text);
    mPager.setCurrentItem(pagerPosition);
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
    outState.putInt(STATE_POSITION, mPager.getCurrentItem());
  }

  private class ImagePagerAdapter extends FragmentStatePagerAdapter {

    public ArrayList<String> fileList;

    public ImagePagerAdapter(FragmentManager fm, ArrayList<String> fileList) {
      super(fm);
      this.fileList = fileList;
    }

    @Override
    public int getCount() {
      return fileList == null ? 0 : fileList.size();
    }

    @Override
    public Fragment getItem(int position) {
      String url = fileList.get(position);
      return ImageDetailFragment.newInstance(url);
    }

  }
}
